﻿var sql = require( 'node-sqlserver' );
var path = require( 'path' );
var async = require( 'async' );

var dbContext = require( path.join( __dirname, "dbContext" ) );

/*
 * 检测流程节点是否已经存在
 */
exports.HasWorkFlowNodesExisted = function ( workflowID, successCall ) {

    dbContext.openNodeConn( function ( conn ) {

        conn.query( "select count(*) as count from workflownode where WorkFlowId = ?",
            [workflowID],
            function ( err, rows ) {

                if ( err ) {
                    err = "HasWorkFlowNodesExisted:" + err;
                    console.log( "Select in DB Error: " + err );
                    
                }

                if ( rows && rows.length > 0 ) {
                    if ( successCall && typeof successCall == "function" ) { successCall( err, rows[0]["count"] > 0 ); }
                }

            });
    });

};

/*
 * 清除某个workflow的全部节点
 */
exports.ClearWorkFlowNodes = function ( workflowID, successCall ) {

    dbContext.openNodeConn( function ( conn ) {

        conn.query( "delete from workflownode where workflowid = ?", [workflowID], function ( err, rows ) {
            if ( err ) {
                err = "ClearWorkFlowNodes:" + err;
                console.log( "Select in DB Error: " + err );
            }

            if ( successCall && typeof successCall == "function" ) { successCall( err, rows ); }

        });

    });

};

exports.InsertWorkFlowNode = function ( Node, successCall ) {

    dbContext.openNodeConn( function ( conn ) {

        conn.query( "insert into WorkFlowNode(WorkFlowId,NodeName,[type],condition,handlerModule,handlerFunction,parameters,transition) values(?,?,?,?,?,?,?,?)",
            [Node.WorkFlowId, Node.NodeName, Node.type, Node.condition, Node.handlerModule, Node.handlerFunction, Node.parameters, Node.transition],
            function ( err, rows ) {
                if ( err ) {
                    err = "InsertWorkFlowNode:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                if ( successCall && typeof successCall == "function" ) { successCall( err, rows ); }
            });
    });
};

/*
 * 批量插入workFlowNode
 */
exports.InsertWorkFlowNodes = function ( Nodes, successCall ) {

    var that = this;

    async.eachSeries( Nodes, function ( node, callback ) {

        that.InsertWorkFlowNode( node, function ( error, result ) {

            if ( error ) {
                console.error( "InsertWorkFlowNodes:" + error );
                console.dir( node );
            } else {
                callback();
            }
        });

    }, function ( err ) {

            if ( successCall && typeof successCall == "function" ) { successCall( err, null ); }

        });

};

/*
 * 根据流程名和节点名获取节点数据
 */
exports.GetNodeByWorkflowNameAndNodeName = function( workflowName, nodeName,condition, successCall){
    
    dbContext.openNodeConn( function ( conn ) { 
        
        conn.query( 
            "select b.* from workflow a left join workflownode b on a.id = b.workflowid where a.name= ? and b.Nodename=? and condition = ?",
            [ workflowName, nodeName, condition ],
            function( err, rows ){

                if ( err ) {
                    err = "GetNodeByWorkflowNameAndNodeName:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                var result = null;

                if( rows && rows.length > 0 ) result = rows[0];

                if ( successCall && typeof successCall == "function" ) { successCall( err, result ); }

            });
            
    });

};

/*
 * 根据流程名和节点名获取下一个节点数据
 */
exports.GetNextNodeInfo = function( workflowName, nodeName,condition, successCall){
    
    dbContext.openNodeConn( function ( conn ) { 
        
        conn.query( 
            "select c.* from workflow a left join workflownode b on a.id = b.workflowid left join workflownode c on b.transition = c.nodename where a.name = ? and b.Nodename = ? and c.condition = ?",
            [workflowName, nodeName, condition],
            function( err, rows ){

                if ( err ) {
                    err = "GetNextNodeInfo:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                var result = null;

                if( rows && rows.length > 0 ) result = rows[0];

                if ( successCall && typeof successCall == "function" ) { successCall( err, result ); }        
            });
            
    });

};
